package com.example.main

import android.Manifest
import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Movie
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings

import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.base.RouterPath
import com.example.common.base.BaseActivity
import com.example.home.HomeFragment
import com.example.live.LiveFragment
import com.example.main.databinding.ActivityMainBinding
import com.example.mine.MineFragment
import com.example.movie.MovieFragment
import com.example.publish.PublishFragment

//@Route(path = RouterPath.PATH_MAIN_PAGE)
class MainActivity : BaseActivity<ActivityMainBinding>() {
    private lateinit var navHostFragment: NavHostFragment
    private lateinit var navController: NavController
    override fun initViews() {
        navHostFragment = supportFragmentManager.findFragmentById(R.id.main_nav_host_fragment) as NavHostFragment
        navController = navHostFragment.navController
        mDatabinding.mainBottomNav.setupWithNavController(navController)
        mDatabinding.mainBottomNav.setOnNavigationItemSelectedListener{
            when(it.itemId){
                R.id.home_fragment_page->{
                    replaceFragment(HomeFragment())
                    true
                }
                R.id.movie_fragment_page->{
                    replaceFragment(MovieFragment())
                    true
                }
                R.id.add_fragment_page->{
                    replaceFragment(PublishFragment())
                    true
                }
                R.id.live_fragment_page->{
                    replaceFragment(LiveFragment())
                    true
                }
                R.id.mine_fragment_page->{
                    replaceFragment(MineFragment())
                    true
                }
                else->false
            }
        }

        checkAppPermission()


    }

    private fun replaceFragment(fragment: Fragment){
        supportFragmentManager.beginTransaction()
            .replace(R.id.main_nav_host_fragment,fragment)
            .commit()
    }
    private fun checkAppPermission() {
        //方案一：系统API
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            requestPermissions(arrayOf(
                Manifest.permission.INTERNET,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.FOREGROUND_SERVICE
            ), 103)
        }

        //方案二：RxPermission
//        RxPermissions(this@MainActivity).request(
//            Manifest.permission.INTERNET,
//            Manifest.permission.WRITE_EXTERNAL_STORAGE,
//            Manifest.permission.CAMERA,
//            Manifest.permission.RECORD_AUDIO,
//            Manifest.permission.READ_PHONE_STATE,
//            Manifest.permission.FOREGROUND_SERVICE
//        ).subscribe()

    }


    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 103){
            //判断返回权限
            if(!permissions.contains(Manifest.permission.INTERNET)){
                showToast("无网络权限，请手动开启")
            }
            if(!permissions.contains(Manifest.permission.WRITE_EXTERNAL_STORAGE)){
                showToast("无外部存储权限，请手动开启")
            }
            if(!permissions.contains(Manifest.permission.CAMERA)){
                showToast("无相机权限，请手动开启")
            }
            if(!permissions.contains(Manifest.permission.RECORD_AUDIO)){
                showToast("无麦克风权限，请手动开启")
            }
            if(!permissions.contains(Manifest.permission.READ_PHONE_STATE)){
                showToast("无手机状态权限，请手动开启")
            }
            if(!permissions.contains(Manifest.permission.FOREGROUND_SERVICE)){
                showToast("无前台服务权限，请手动开启")
            }
        }
    }

    /**
     * 打开应用详情页面
     */
    private fun openAppDetailPage(){
        var intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
        intent.data = Uri.fromParts("package", packageName, null)
        this@MainActivity.startActivity(intent)
    }

    override fun getLayoutId(): Int =R.layout.activity_main

}